#include "ChangePINDlg.h"
#include "DatabaseAccess.h"
#include "CardBussiness.h"
#include "MenuDlg.h"
#include "common.h"
#include "resource.h"

LRESULT CALLBACK ChangePINDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
    CChangePINDlg *Obj;
    if (message==WM_INITDIALOG)
    {	
        Obj=(CChangePINDlg*)lParam;
        SetWindowLong(hWnd,GWL_USERDATA,(LONG)Obj);
    }
    Obj=(CChangePINDlg*)GetWindowLong(hWnd,GWL_USERDATA);
    if (Obj)
        return Obj->DialogProc(hWnd, message, wParam, lParam);
    return 0;
}

CChangePINDlg::CChangePINDlg(HINSTANCE hInstance, HWND hParent)
{
    DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG_CHANGE_PIN), hParent, (DLGPROC)ChangePINDlgProc, (LPARAM)this);
}

CChangePINDlg::~CChangePINDlg(void)
{
    EndDialog(m_hDlg,0);
}

LRESULT CChangePINDlg::DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HWND hEditNewPIN;
    CDatabaseAccess objDAO;
    CCardBussiness objCardBuss;
    static int nCountInputNewPIN = 0;

    switch (message) 
    {
    case WM_INITDIALOG:
        {	
            hEditNewPIN = GetDlgItem(hWnd, IDC_EDIT_NEW_PIN);
            m_hDlg = hWnd;	
            SetFocus(hEditNewPIN);
            return FALSE;
        }		
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {	
        case IDOK:
            {     
                HWND hReminderNewPIN = GetDlgItem(m_hDlg, IDC_STATIC_REMINDER_NEW_PIN);
                hEditNewPIN = GetDlgItem(hWnd, IDC_EDIT_NEW_PIN);
                if (nCountInputNewPIN == 0)
                {         
                    GetWindowText(hEditNewPIN, g_sNewPIN, 8);
                    if (strcmp(g_sNewPIN, g_Card.GetPin()) == 0)
                    {
                        MessageBox(hWnd, NEW_PIN_INVALID, PIN_INVALID_CAPTION, MB_ICONERROR);
                        SetWindowText(hEditNewPIN,"");
                        SetFocus(hEditNewPIN);
                    }
                    else if (strlen(g_sNewPIN) == LENGTH_PIN)
                    {
                        SetWindowText(hEditNewPIN,"");
                        SetFocus(hEditNewPIN);
                        SetWindowText(hReminderNewPIN, STATIC_REINPUT_NEW_PIN);
                        nCountInputNewPIN++;
                        break;
                    }
                    else 
                    {
                        MessageBox(hWnd, LENGTH_NEW_PIN_INVALID, PIN_INVALID_CAPTION, MB_ICONERROR);
                        SetWindowText(hEditNewPIN,"");
                        SetFocus(hEditNewPIN);
                    }
                }  
                if (nCountInputNewPIN == 1)
                {
                    GetWindowText(hEditNewPIN, g_sNewPINRepeat, 8);
                    if(objCardBuss.CheckNewPIN(g_sNewPIN, g_sNewPINRepeat))
                    {
                        nCountInputNewPIN = 0;
                        MessageBox(hWnd, CHANGE_PIN_SUCCESS, CHANGE_PIN_SUCCESS_CAPTION, MB_ICONINFORMATION);
                        EndDialog(hWnd, 0);
                        MenuDlg objMenuDlg(hGlobalInst, NULL);
                    }
                    else
                    {
                        MessageBox(hWnd, NEW_PIN_REPEAT_INVALID, PIN_INVALID_CAPTION, MB_ICONERROR); 
                        SetWindowText(hReminderNewPIN, STATIC_INPUT_NEW_PIN);
                        SetWindowText(hEditNewPIN,"");
                        SetFocus(hEditNewPIN);
                        nCountInputNewPIN = 0;
                    }
                }
                return TRUE;
            }  
        case IDCANCEL:
            nCountInputNewPIN = 0;
            EndDialog(hWnd, 0);
            MenuDlg objMenuDlg(hGlobalInst, NULL);
            return TRUE;
        }
        break;
    default:
        return FALSE;
    }
    return TRUE;
}
